<?php
/**
 * taobaoSpider.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/10/23- 1:29 下午
 */

namespace common\drivers\spiderProduct;


use common\exception\CopyException;
use common\constants\CloudProductConst;
use common\constants\PlatformConst;
use common\drivers\CookieDriver;
use common\drivers\interfaces\SpiderProductInterface;
use common\exception\DriverException;
use common\helpers\CookiesHelper;
use common\helpers\StringHelper;
use common\helpers\UrlsHelper;
use common\services\CloudProduct;
use console\components\helpers\ConsoleHelper;
use QL\QueryList;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

class TaobaoSpider extends BaseSpider implements SpiderProductInterface
{

    protected $skuStock = [];

    protected $detailUrl;


    /**
     * 万邦请求url 获取淘宝商品数据地址
     * @var string
     */
    protected $requestUrl = 'https://api-gw.onebound.cn/taobao/item_get/';
    /**
     * 失败重试3次
     * @var int
     */
    protected $tryTimes = 3;

    /**
     * 抓取内容
     * @since 增加失败重复功能
     * @inheritDoc
     * @throws CopyException
     */
    public function getContents(string $url): string
    {
        $contentTimes = 0;
        while($contentTimes < $this->tryTimes)
        {

            $num_iid = UrlsHelper::getUrlPrimaryKey(PlatformConst::PLATFORM_TB, $url);
            $this->requestUrl .= '?key='.$this->key.'&&num_iid='.$num_iid.'&is_promotion=1&&lang=zh-CN&secret='.$this->secret;
            $resources = $this->spiderContent($this->requestUrl);

            $this->detailUrl = $url;
            if(!$resources)
            {
                throw new CopyException('获取数据源失败');
            }
            $contents = $resources->getBody()->getContents();
            if(!$contents)
            {
                $contentTimes++;
                ConsoleHelper::runInfo("抓取淘宝商品失败，即将重试第{$contentTimes}次");
                continue;
            }
            //评率超限制，再次请求一次
            $formatContents = StringHelper::jsonDecode($contents);
            if($formatContents && isset($formatContents['error_code']) && $formatContents['error_code'] == '4008')
            {
                $contentTimes++;
                sleep(1);
                ConsoleHelper::runInfo("抓取淘宝商品评率超限制，即将重试第{$contentTimes}次");
                continue;
            }

            $this->resources = $contents;
            break;
        }
//        $jsonStr = '
//{"item":{"num_iid":"628113014757","title":"性感辣妹风拉链半高领包臀短裙女秋冬长袖针织小黑裙紧身连衣裙潮","desc_short":"","price":"79","total_price":"","suggestive_price":"","orginal_price":"199.00","nick":"七月与她nana","num":72011,"detail_url":"https:\/\/item.taobao.com\/item.htm?id=628113014757","pic_url":"\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01XgcMPA1MCmiiOaRxg_!!4046351399-0-lubanu-s.jpg","brand":"紫沫","brandId":"96934440","rootCatId":"16","cid":"50010850","desc":"\n  <p><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01Q5I5xW1MCmiml3Uzc_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01udJh0X1MCmik7B214_!!4046351399.jpg\"  \/><\/p> \n  <p><img alt=\"qun-恢复的(1)_03.jpg\" border=\"0\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01Kvcvc81MCmiiDnFQx_!!4046351399.jpg\" usemap=\"#Map4156156156\" \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i1\/4046351399\/O1CN01WUgNUR1MCmilHxftK_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01hab1gU1MCmiZPSZk0_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01IUPlJv1MCmiml4m1B_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01DeqEP81MCmifA03H9_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN011EWs6Z1MCmikQ7khm_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01r1Zbje1MCmin5Jr7l_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01V3NUoQ1MCmifc5Bab_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01SIj51u1MCmiZPWX7w_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN016CmvWe1MCmilv8o03_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01y36jRt1MCmiiDlIth_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01YmHOdL1MCmiml6eSE_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i1\/4046351399\/O1CN01UVMCxl1MCmihJdaRL_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01295Dcs1MCmilv9PTM_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01E1gDM51MCmilvCUhp_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN012XB3C91MCmik7DFKI_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01ZDpY0y1MCmijSHA1x_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01n8xHRa1MCmilvCd2M_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01JtPien1MCmihczGp6_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01ID1yQY1MCmin5KKJc_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN011HKC1Y1MCmijSI6G5_!!4046351399.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN011jOazZ1MCmin5K7qJ_!!4046351399.jpg\"  \/><map name=\"Map4156156156\">   <\/map><\/p>\n  ","item_imgs":[{"url":"\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01XgcMPA1MCmiiOaRxg_!!4046351399-0-lubanu-s.jpg"},{"url":"\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01WWLYlS1MCmiiOaeQm_!!4046351399-0-lubanu-s.jpg"},{"url":"\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01NOqenl1MCmihdpAWF_!!4046351399-0-lubanu-s.jpg"},{"url":"\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01AaPEzz1MCmiiOdbP1_!!4046351399-0-lubanu-s.jpg"},{"url":"\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01VImK4l1MCmifdWjy3_!!4046351399-0-lubanu-s.jpg"}],"item_weight":"","post_fee":"","express_fee":null,"ems_fee":"","shipping_to":"","video":[],"sample_id":"","props_name":"20509:28314:尺码:S;20509:28315:尺码:M;1627207:28341:颜色分类:黑色;1627207:388530715:颜色分类:baby蓝;1627207:48381252:颜色分类:薄荷绿;1627207:3232478:颜色分类:深灰色;1627207:4513375:颜色分类:宝石蓝","prop_imgs":{"prop_img":[{"properties":"1627207:28341","url":"\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01W2qX4z1MCmilHwfhA_!!4046351399.jpg"},{"properties":"1627207:388530715","url":"\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01vPEU7k1MCmik7EOAG_!!4046351399.jpg"},{"properties":"1627207:48381252","url":"\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01TFw35H1MCmik7FjJ5_!!4046351399.jpg"},{"properties":"1627207:3232478","url":"\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01AWAHp01MCmighuL1B_!!4046351399.jpg"},{"properties":"1627207:4513375","url":"\/\/img.alicdn.com\/imgextra\/i1\/4046351399\/O1CN01dkUVL41MCminaXS8q_!!4046351399.jpg"}]},"props_imgs":{"prop_img":[{"properties":"1627207:28341","url":"\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01W2qX4z1MCmilHwfhA_!!4046351399.jpg"},{"properties":"1627207:388530715","url":"\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01vPEU7k1MCmik7EOAG_!!4046351399.jpg"},{"properties":"1627207:48381252","url":"\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01TFw35H1MCmik7FjJ5_!!4046351399.jpg"},{"properties":"1627207:3232478","url":"\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01AWAHp01MCmighuL1B_!!4046351399.jpg"},{"properties":"1627207:4513375","url":"\/\/img.alicdn.com\/imgextra\/i1\/4046351399\/O1CN01dkUVL41MCminaXS8q_!!4046351399.jpg"}]},"property_alias":"","props":[{"name":"品牌","value":"紫沫"},{"name":"适用年龄","value":"18-24周岁"},{"name":"尺码","value":"S,M"},{"name":"图案","value":"纯色"},{"name":"风格","value":"街头"},{"name":"街头","value":"欧美"},{"name":"领型","value":"半高领"},{"name":"腰型","value":"高腰"},{"name":"衣门襟","value":"拉链"},{"name":"颜色分类","value":"黑色,深灰色,baby蓝,薄荷绿,宝石蓝"},{"name":"袖型","value":"常规"},{"name":"组合形式","value":"单件"},{"name":"裙型","value":"A字裙"},{"name":"年份季节","value":"2020年秋季"},{"name":"袖长","value":"长袖"},{"name":"裙长","value":"短裙"},{"name":"廓形","value":"A型"}],"total_sold":"15412","skus":{"sku":[{"price":"79","total_price":0,"orginal_price":"79","properties":"20509:28314;1627207:28341","properties_name":"20509:28314:尺码:S;1627207:28341:颜色分类:黑色","quantity":"9092","sku_id":"4458945381439"},{"price":"79","total_price":0,"orginal_price":"79","properties":"20509:28315;1627207:28341","properties_name":"20509:28315:尺码:M;1627207:28341:颜色分类:黑色","quantity":"7142","sku_id":"4458945381440"},{"price":"79","total_price":0,"orginal_price":"79","properties":"20509:28314;1627207:388530715","properties_name":"20509:28314:尺码:S;1627207:388530715:颜色分类:baby蓝","quantity":"9298","sku_id":"4458945381441"},{"price":"79","total_price":0,"orginal_price":"79","properties":"20509:28315;1627207:388530715","properties_name":"20509:28315:尺码:M;1627207:388530715:颜色分类:baby蓝","quantity":"9365","sku_id":"4458945381442"},{"price":"79","total_price":0,"orginal_price":"79","properties":"20509:28314;1627207:48381252","properties_name":"20509:28314:尺码:S;1627207:48381252:颜色分类:薄荷绿","quantity":"0","sku_id":"4458945381443"},{"price":"79","total_price":0,"orginal_price":"79","properties":"20509:28315;1627207:48381252","properties_name":"20509:28315:尺码:M;1627207:48381252:颜色分类:薄荷绿","quantity":"1","sku_id":"4458945381444"},{"price":"79","total_price":0,"orginal_price":"79","properties":"20509:28314;1627207:3232478","properties_name":"20509:28314:尺码:S;1627207:3232478:颜色分类:深灰色","quantity":"9063","sku_id":"4461646925354"},{"price":"79","total_price":0,"orginal_price":"79","properties":"20509:28315;1627207:3232478","properties_name":"20509:28315:尺码:M;1627207:3232478:颜色分类:深灰色","quantity":"8989","sku_id":"4461646925355"},{"price":"79","total_price":0,"orginal_price":"79","properties":"20509:28314;1627207:4513375","properties_name":"20509:28314:尺码:S;1627207:4513375:颜色分类:宝石蓝","quantity":"9580","sku_id":"4634373871712"},{"price":"79","total_price":0,"orginal_price":"79","properties":"20509:28315;1627207:4513375","properties_name":"20509:28315:尺码:M;1627207:4513375:颜色分类:宝石蓝","quantity":"9481","sku_id":"4634373871713"}]},"seller_id":"4046351399","sales":10000,"shop_id":"538758274","props_list":{"20509:28314":"尺码:S","20509:28315":"尺码:M","1627207:28341":"颜色分类:黑色","1627207:388530715":"颜色分类:baby蓝","1627207:48381252":"颜色分类:薄荷绿","1627207:3232478":"颜色分类:深灰色","1627207:4513375":"颜色分类:宝石蓝"},"seller_info":{"nick":"七月与她nana","item_score":"4.8 ","score_p":"4.8 ","delivery_score":"4.8 ","shop_type":"C","user_num_id":"4046351399","sid":"538758274","title":"张小南 NANNAN","zhuy":"https:\/\/shop538758274.taobao.com\/","shop_name":"张小南 NANNAN"},"tmall":false,"error":"","fav_count":"68854","fans_count":"49.5万","location":"浙江杭州","data_from":"tbf","props_img":{"1627207:28341":"\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01W2qX4z1MCmilHwfhA_!!4046351399.jpg","1627207:388530715":"\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01vPEU7k1MCmik7EOAG_!!4046351399.jpg","1627207:48381252":"\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01TFw35H1MCmik7FjJ5_!!4046351399.jpg","1627207:3232478":"\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01AWAHp01MCmighuL1B_!!4046351399.jpg","1627207:4513375":"\/\/img.alicdn.com\/imgextra\/i1\/4046351399\/O1CN01dkUVL41MCminaXS8q_!!4046351399.jpg"},"desc_img":["http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01Q5I5xW1MCmiml3Uzc_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01udJh0X1MCmik7B214_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01Kvcvc81MCmiiDnFQx_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i1\/4046351399\/O1CN01WUgNUR1MCmilHxftK_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01hab1gU1MCmiZPSZk0_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01IUPlJv1MCmiml4m1B_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01DeqEP81MCmifA03H9_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN011EWs6Z1MCmikQ7khm_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01r1Zbje1MCmin5Jr7l_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01V3NUoQ1MCmifc5Bab_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01SIj51u1MCmiZPWX7w_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN016CmvWe1MCmilv8o03_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01y36jRt1MCmiiDlIth_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01YmHOdL1MCmiml6eSE_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i1\/4046351399\/O1CN01UVMCxl1MCmihJdaRL_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01295Dcs1MCmilv9PTM_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01E1gDM51MCmilvCUhp_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN012XB3C91MCmik7DFKI_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01ZDpY0y1MCmijSHA1x_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN01n8xHRa1MCmilvCd2M_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i3\/4046351399\/O1CN01JtPien1MCmihczGp6_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN01ID1yQY1MCmin5KKJc_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i2\/4046351399\/O1CN011HKC1Y1MCmijSI6G5_!!4046351399.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/4046351399\/O1CN011jOazZ1MCmin5K7qJ_!!4046351399.jpg"],"shop_item":[],"relate_items":[]},"secache":"a996e401bc02cbab6974785c04d53b04","secache_time":1609053386,"secache_date":"2020-12-27 15:16:26","translate_status":"","translate_time":0,"language":{"default_lang":"cn","current_lang":"cn"},"error":"","reason":"","error_code":"0000","cache":0,"api_info":"today:8 max:10","execution_time":1.01,"server_time":"Beijing\/2020-12-27 15:16:26","client_ip":"36.5.119.35","call_args":{"num_iid":"628113014757","is_promotion":"1"},"api_type":"taobao","translate_language":"zh-CN","translate_engine":"google_cn","server_memory":"5.21MB","request_id":"gw-4.5fe834c9a0f21"}';

//        $jsonStr = '
//{"item":{"num_iid":"625102119022","title":"乐高收纳盒整理箱子宝宝储物柜零食塑料零件抽屉儿童积木玩具分类","desc_short":"","price":"53","total_price":"","suggestive_price":"","orginal_price":"106.00","nick":"raoxf0303","num":8875,"detail_url":"https:\/\/item.taobao.com\/item.htm?id=625102119022","pic_url":"\/\/img.alicdn.com\/imgextra\/i1\/2651467722\/O1CN016Cwv9i26uj29dYqsf_!!2651467722-0-lubanu-s.jpg","brand":"","brandId":null,"rootCatId":"122928002","cid":"50002711","desc":"\n  <p><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01LnSfHd26uj297Vwt7_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i1\/2651467722\/O1CN01AdjzQT26uj2Gkj8He_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01s55eUc26uj2FewmN6_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01I9SEYp26uj2Bb6per_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01M5z32626uj2F0gvC7_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN015cO8Nt26uj2GRmTjj_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN0126Hu3B26uj2BbFUzU_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01oeC47z26uj2DrxE6S_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i1\/2651467722\/O1CN01zRUeJP26uj2CF4Vfc_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01IvMuk226uj2DrxhC1_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i3\/2651467722\/O1CN01dnHB3G26uj2FexW7Y_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01ikSwN426uj2Bb8VaJ_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01iSJHJ926uj297WkqL_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01pzwZrw26uj2F0ifI0_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i1\/2651467722\/O1CN01xd2Nuz26uj2CF5a9s_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01fLdSuf26uj2BE49Lj_!!2651467722.jpg\"  \/><img align=\"absmiddle\" src=\"http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01ZKJPlU26uj2C6sITt_!!2651467722.jpg\"  \/><\/p>\n  ","item_imgs":[{"url":"\/\/img.alicdn.com\/imgextra\/i1\/2651467722\/O1CN016Cwv9i26uj29dYqsf_!!2651467722-0-lubanu-s.jpg"},{"url":"\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01E6asGc26uj24wS2Yg_!!2651467722-0-lubanu-s.jpg"},{"url":"\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01sHmKLJ26uj2CjoJ6S_!!2651467722-0-lubanu-s.jpg"},{"url":"\/\/img.alicdn.com\/imgextra\/i1\/2651467722\/O1CN01pmrthJ26uj2HBFmMH_!!2651467722-0-lubanu-s.jpg"},{"url":"\/\/img.alicdn.com\/imgextra\/i1\/2651467722\/O1CN01y5yTGx26uj2HyniZ8_!!2651467722-0-lubanu-s.jpg"}],"item_weight":"","post_fee":"","express_fee":null,"ems_fee":"","shipping_to":"","video":[],"sample_id":"","props_name":"21433:444434628:规格:蓝底白框;21433:425282363:规格:白底灰框;1627207:69649306:颜色分类:3抽屉;1627207:83013380:颜色分类:5抽屉;1627207:90273757:颜色分类:7抽屉;1627207:9305057812:颜色分类:三件装【3抽屉+5抽屉+7抽屉】","prop_imgs":{"prop_img":[{"properties":"1627207:69649306","url":"\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01RYlt7y26uj2GRnLi9_!!2651467722.jpg"},{"properties":"1627207:83013380","url":"\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01QuORxT26uj2GRoQDn_!!2651467722.jpg"},{"properties":"1627207:90273757","url":"\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01kGVz6426uj2DJ8idR_!!2651467722.jpg"},{"properties":"1627207:9305057812","url":"\/\/img.alicdn.com\/imgextra\/i3\/2651467722\/O1CN01tXpgfV26uj2IIMqAs_!!2651467722.jpg"}]},"props_imgs":{"prop_img":[{"properties":"1627207:69649306","url":"\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01RYlt7y26uj2GRnLi9_!!2651467722.jpg"},{"properties":"1627207:83013380","url":"\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01QuORxT26uj2GRoQDn_!!2651467722.jpg"},{"properties":"1627207:90273757","url":"\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01kGVz6426uj2DJ8idR_!!2651467722.jpg"},{"properties":"1627207:9305057812","url":"\/\/img.alicdn.com\/imgextra\/i3\/2651467722\/O1CN01tXpgfV26uj2IIMqAs_!!2651467722.jpg"}]},"property_alias":"","props":[{"name":"材质","value":"塑料"},{"name":"风格","value":"北欧风格"},{"name":"规格","value":"蓝底白框,白底灰框"},{"name":"流行元素","value":"软萌治愈系"},{"name":"适用空间","value":"儿童房"},{"name":"颜色分类","value":"3抽屉,5抽屉,7抽屉,三件装【3抽屉+5抽屉+7抽屉】"},{"name":"适用人群","value":"儿童"},{"name":"净重","value":"1.05kg"}],"total_sold":"1799","skus":{"sku":[{"price":"53","total_price":0,"orginal_price":"53","properties":"21433:444434628;1627207:69649306","properties_name":"21433:444434628:规格:蓝底白框;1627207:69649306:颜色分类:3抽屉","quantity":"896","sku_id":"4424218973456"},{"price":"53","total_price":0,"orginal_price":"53","properties":"21433:425282363;1627207:69649306","properties_name":"21433:425282363:规格:白底灰框;1627207:69649306:颜色分类:3抽屉","quantity":"1223","sku_id":"4424218973457"},{"price":"56","total_price":0,"orginal_price":"56","properties":"21433:444434628;1627207:83013380","properties_name":"21433:444434628:规格:蓝底白框;1627207:83013380:颜色分类:5抽屉","quantity":"871","sku_id":"4424218973458"},{"price":"56","total_price":0,"orginal_price":"56","properties":"21433:425282363;1627207:83013380","properties_name":"21433:425282363:规格:白底灰框;1627207:83013380:颜色分类:5抽屉","quantity":"1191","sku_id":"4424218973459"},{"price":"58","total_price":0,"orginal_price":"58","properties":"21433:444434628;1627207:90273757","properties_name":"21433:444434628:规格:蓝底白框;1627207:90273757:颜色分类:7抽屉","quantity":"1237","sku_id":"4424218973460"},{"price":"58","total_price":0,"orginal_price":"58","properties":"21433:425282363;1627207:90273757","properties_name":"21433:425282363:规格:白底灰框;1627207:90273757:颜色分类:7抽屉","quantity":"1396","sku_id":"4424218973461"},{"price":"153","total_price":0,"orginal_price":"153","properties":"21433:444434628;1627207:9305057812","properties_name":"21433:444434628:规格:蓝底白框;1627207:9305057812:颜色分类:三件装【3抽屉+5抽屉+7抽屉】","quantity":"863","sku_id":"4424218973462"},{"price":"153","total_price":0,"orginal_price":"153","properties":"21433:425282363;1627207:9305057812","properties_name":"21433:425282363:规格:白底灰框;1627207:9305057812:颜色分类:三件装【3抽屉+5抽屉+7抽屉】","quantity":"1198","sku_id":"4424218973463"}]},"seller_id":"2651467722","sales":1000,"shop_id":"131262577","props_list":{"21433:444434628":"规格:蓝底白框","21433:425282363":"规格:白底灰框","1627207:69649306":"颜色分类:3抽屉","1627207:83013380":"颜色分类:5抽屉","1627207:90273757":"颜色分类:7抽屉","1627207:9305057812":"颜色分类:三件装【3抽屉+5抽屉+7抽屉】"},"seller_info":{"nick":"raoxf0303","item_score":"4.8 ","score_p":"4.8 ","delivery_score":"4.8 ","shop_type":"C","user_num_id":"2651467722","sid":"131262577","title":"雅居乐收纳","zhuy":"https:\/\/shop131262577.taobao.com\/","shop_name":"雅居乐收纳"},"tmall":false,"error":"","fav_count":"4326","fans_count":"31.5万","location":"广东深圳","data_from":"tbf","props_img":{"1627207:69649306":"\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01RYlt7y26uj2GRnLi9_!!2651467722.jpg","1627207:83013380":"\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01QuORxT26uj2GRoQDn_!!2651467722.jpg","1627207:90273757":"\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01kGVz6426uj2DJ8idR_!!2651467722.jpg","1627207:9305057812":"\/\/img.alicdn.com\/imgextra\/i3\/2651467722\/O1CN01tXpgfV26uj2IIMqAs_!!2651467722.jpg"},"desc_img":["http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01LnSfHd26uj297Vwt7_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i1\/2651467722\/O1CN01AdjzQT26uj2Gkj8He_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01s55eUc26uj2FewmN6_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01I9SEYp26uj2Bb6per_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01M5z32626uj2F0gvC7_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN015cO8Nt26uj2GRmTjj_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN0126Hu3B26uj2BbFUzU_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01oeC47z26uj2DrxE6S_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i1\/2651467722\/O1CN01zRUeJP26uj2CF4Vfc_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01IvMuk226uj2DrxhC1_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i3\/2651467722\/O1CN01dnHB3G26uj2FexW7Y_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01ikSwN426uj2Bb8VaJ_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i2\/2651467722\/O1CN01iSJHJ926uj297WkqL_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01pzwZrw26uj2F0ifI0_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i1\/2651467722\/O1CN01xd2Nuz26uj2CF5a9s_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01fLdSuf26uj2BE49Lj_!!2651467722.jpg","http:\/\/img.alicdn.com\/imgextra\/i4\/2651467722\/O1CN01ZKJPlU26uj2C6sITt_!!2651467722.jpg"],"shop_item":[],"relate_items":[]},"secache":"7192f354aca2fb731148a7d181113a87","secache_time":1609056960,"secache_date":"2020-12-27 16:16:00","translate_status":"","translate_time":0,"language":{"default_lang":"cn","current_lang":"cn"},"error":"","reason":"","error_code":"0000","cache":0,"api_info":"today:10 max:10","execution_time":1.224,"server_time":"Beijing\/2020-12-27 16:16:00","client_ip":"36.5.119.35","call_args":{"num_iid":"625102119022","is_promotion":"1"},"api_type":"taobao","translate_language":"zh-CN","translate_engine":"google_cn","server_memory":"5.19MB","request_id":"gw-4.5fe842bf16c58"}';

        //数据没有获取到抛出异常
        if(!$this->resources)
        {
            throw new CopyException('获取数据源失败');
        }

//        $this->resources = $jsonStr;


//        print_r($this->resources); exit;
//        $this->resources = $jsonStr;//StringHelper::jsonDecode($jsonStr);
        return $this->resources;

    }

    /**
     *
     * 获取详细内容
     * @inheritDoc
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \common\exception\DriverException
     */
    public function formatContents(): array
    {
        $this->resources = StringHelper::jsonDecode($this->resources);

        if(!isset($this->resources['item']) || empty($this->resources['item']))
        {
            throw new DriverException('数据源不存在或已下架');
        }

        /**
         * 标题和价格为空说明商品已经不存在了
         */
        if(empty($this->resources['item']['title']) || empty($this->resources['item']['price']))
        {
            throw new DriverException('数据源不存在');
        }

        $this->productInfo['platform'] = PlatformConst::PLATFORM_TB;

        $this->productInfo['mall_name'] = isset($this->resources['item']['seller_info']['shop_name']) && !empty($this->resources['item']['seller_info']['shop_name']) ?
            trim($this->resources['item']['seller_info']['shop_name']) : $this->resources['item']['nick'];

        $this->productInfo['mall_id'] = $this->resources['item']['shop_id'];

        $this->productInfo['title'] = $this->resources['item']['title'] ?? '';
        $this->productInfo['prices'] = $this->resources['item']['price'];

        $pics = ArrayHelper::getColumn($this->resources['item']['item_imgs'],'url');

        if(is_array($pics))
        {
            foreach($pics as $k => $pic)
            {
                if(!is_string($pic))
                {
                    continue;
                }
                $pics[$k] = $this->_imgUrl($pic);
            }
        }

        $this->productInfo['pics'] = $pics;
        $this->productInfo['num_iid'] = $this->resources['item']['num_iid'];

        $this->productInfo['main_pic'] = is_string($this->resources['item']['pic_url']) ? $this->_imgUrl($this->resources['item']['pic_url']) : '';

        if(is_array($this->resources['item']['video']) &&  count($this->resources['item']['video']))
        {
            $this->productInfo['video'] = $this->resources['item']['video'];
        }

        if(isset($this->resources['item']['desc_img']) && is_array($this->resources['item']['desc_img']))
        {
            $this->productInfo['detail_gallery'] = $this->resources['item']['desc_img'];
        }

        if(is_array($this->resources['item']['props']) && count($this->resources['item']['props']))
        {
            $attr = [];
            foreach ($this->resources['item']['props'] as $k =>$item)
            {
                array_push($attr,['key'=>$item['name'],'value'=>$item['value']]);
            }
            $this->productInfo['attr'] = $attr;
        }
        $this->productInfo['cid'] = $this->resources['item']['cid'];


        $this->productInfo['pc_desc'] = $this->resources['item']['desc'] ?? '';

        $this->setSku();

        $this->setExtend();


        return $this->productInfo;

    }

    /**
     * 设置扩展信息
     */
    protected function setExtend()
    {
        $this->productInfo['extend_data'] = [
            'fav_count' => $this->resources['item']['fav_count'] ?? 0, //收藏量
            'total_sold' => $this->resources['item']['total_sold'] ?? 0, //评论数
            'sales' => $this->resources['item']['sales'] ?? 0, //销量
            'post_fee' => $this->resources['item']['post_fee'] ?? 0, //物流费用
            'express_fee' => $this->resources['item']['express_fee'] ?? 0, //快递费用
            'location' => $this->resources['item']['location'] ?? '', //发货地
        ];
    }


    /**
     * 图片添加前缀https:
     * @param string $imgPath
     * @return string
     */
    private function _imgUrl(string $imgPath)
    {
        $imgPath = str_replace(['https:','http:'],'',$imgPath);
        return 'https:'.$imgPath;
    }

    /**
     * 设置sku信息
     */
    protected function setSku()
    {
        $propsList = $propsImgList =[];
        if(isset($this->resources['item']['props_list']) && is_array($this->resources['item']['props_list']))
        {
            $propsList = $this->resources['item']['props_list'];
        }
        if(isset($this->resources['item']['props_img']) && is_array($this->resources['item']['props_img']))
        {
            $propsImgList = $this->resources['item']['props_img'];
        }
        /**
         * skuImg
         */
        if(count($propsImgList))
        {
            foreach ($propsImgList as $k => $propImg)
            {
                if(!isset($propsList[$k])) continue;
                $tmp = [];
                $listKeyList = explode(':',$propsList[$k]);
                $tmp['name'] = $listKeyList[0] ?? '';
                unset($listKeyList[0]);
                $tmp['value'] = implode(':',$listKeyList);
                $tmp['skuImg'] = is_string($propImg) ? $this->_imgUrl($propImg) : '';
                $tmp['key'] = $k;
                array_push($this->productInfo['sku_img'],$tmp);
            }
        }
        /**
         * sku
         */
        if(isset($this->resources['item']['skus']['sku']) && is_array($this->resources['item']['skus']['sku']))
        {
            foreach($this->resources['item']['skus']['sku'] as $k => $sku)
            {
                $tmp = [];
                $tmp['skuId'] = $sku['sku_id'];
                $tmp['stock'] = $sku['quantity'];
                $tmp['price'] = $sku['orginal_price']; //原价
                $tmp['sale_price'] = $sku['price']; //销售价
                $tmp['cover'] = '';
                $tmp['properties'] = StringHelper::jsonEncode(explode(';',$sku['properties']));
                $propertiesList = explode(';',$sku['properties']);
                $tmp['properties_name'] = [];
                foreach($propertiesList as $props)
                {
                    if(isset($propsImgList[$props]) && is_string($propsImgList[$props]))
                    {
                        $tmp['cover'] = $this->_imgUrl($propsImgList[$props]);
                    }
                    if(isset($propsList[$props]) && is_string($propsList[$props]))
                    {
                        array_push($tmp['properties_name'],$propsList[$props]);
                    }
                }
                $tmp['properties_name'] = StringHelper::jsonEncode($tmp['properties_name']);
                array_push($this->productInfo['skus'],$tmp);
            }
        }

    }


    /**
     * @inheritDoc
     */
    public function saveDb()
    {

    }

    /**
     * @inheritDoc
     */
    public function setErrorInfo(string $msg = '')
    {

    }

    /**
     * @inheritDoc
     */
    public function getErrorInfo()
    {
        return $this->errorInfo;
    }
}
